package day6.homework;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

//定义一个List存储10个User对象  数据可以 自行添加
//   通过迭代器的方式遍历出来集合的每个元素 中的id name sex age属性
public class TestDemo1 {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>(10);
        Random r = new Random();
        for (int i = 1; i <= 10; i++) {
            String name = getName(r);
            String sex = r.nextInt(2)==1?"男":"女";
            int age = r.nextInt(6)+18;
            UserInfo userInfo = new UserInfo(i, sex, age);
            User user = new User(i, name, userInfo);
            userList.add(user);
        }
        Iterator<User> it = userList.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
}
    public static String getName(Random random){
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 3 ; i++) {
          sb.append((char)(random.nextInt(26)+97));
        }
        return sb.toString();
    }
    static class User {
        int id;
        String name;
        UserInfo userinfo;

        public User(int id, String name, UserInfo userinfo) {
            this.id = id;
            this.name = name;
            this.userinfo = userinfo;
        }

        @Override
        public String toString() {
            return "id:" + id + ", name:" + name  +
                    ", sex:" + userinfo.sex +",age:"+userinfo.age;
        }
    }

    static class UserInfo {
        int id;
        String sex;
        int age;

        public UserInfo(int id, String sex, int age) {
            this.id = id;
            this.sex = sex;
            this.age = age;
        }

    }
}